/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.ecard.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.ims.modules.ecard.entity.OrganInfo;
import com.boxin.ims.modules.ecard.service.OrganInfoService;

/**
 * 单位信息Controller
 * @author Jakemanse
 * @version 2013-06-10
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/ecard/organInfo")
public class OrganInfoController extends BaseController {

	@Autowired
	private OrganInfoService organInfoService;
	
	@ModelAttribute
	public OrganInfo get(@RequestParam(required=false) Long id) {
		if (id != null){
			return organInfoService.get(id);
		}else{
			return new OrganInfo();
		}
	}
	
	@RequiresPermissions("ecard:organInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(OrganInfo organInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			organInfo.setUser(user);
		}
		
        Page<OrganInfo> page = organInfoService.find(new Page<OrganInfo>(request, response), organInfo); 
        model.addAttribute("page", page);
		return "modules/ecard/organInfoList";
	}

	@RequiresPermissions("ecard:organInfo:view")
	@RequestMapping(value = "form")
	public String form(OrganInfo organInfo, Model model) {
		User user = UserUtils.getUser();
		
		if(organInfo.getId() != null){
			organInfo = organInfoService.getByUserId(user.getId());
		}
		
		if(organInfo.getUser() == null ){
			organInfo.setUser(user);
		}
		model.addAttribute("organInfo", organInfo);
		return "modules/ecard/organInfoForm";
	}

	@RequiresPermissions("ecard:organInfo:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("file") MultipartFile image,OrganInfo organInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, organInfo)){
			return form(organInfo, model);
		}
		
		
		String logoPath = Constants.getEcardResourceSavePath();
		String sname = "";
		String fname = "";
		try {
			if(image != null && image.getBytes().length>0){
				sname = image.getOriginalFilename();
				String fileType = sname.substring(sname.lastIndexOf("."));
				fname  = UserUtils.getUser().getLoginName()+"_ecard_logo_"+System.currentTimeMillis()+fileType;
				File file = new File(logoPath+fname);
				System.out.println("保存文件:"+logoPath+fname);
					image.transferTo(file);
					organInfo.setLogo(file.getPath());
			
			}
		} catch (Exception e) {
			System.out.println("上传文件失败:"+logoPath+fname);
			e.printStackTrace();
		}
		
		
		organInfoService.save(organInfo);
		addMessage(redirectAttributes, "保存单位信息'" + organInfo.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/ecard/organInfo/?repage";
	}
	
	@RequiresPermissions("ecard:organInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		organInfoService.delete(id);
		addMessage(redirectAttributes, "删除单位信息成功");
		return "redirect:"+Global.ADMIN_PATH+"/ecard/organInfo/?repage";
	}

}
